package controller;

import com.github.pagehelper.PageInfo;
import entity.CustomerLoss;
import entity.LayRequest;
import entity.MyResult;
import entity.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import service.CustomerLossService;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/loss")
public class CustomerLossController {
    @Autowired
    public CustomerLossService customerLossService;

    @RequestMapping("/toadd")
    public String toadd(ModelMap modelMap){
        //查询所有的角色信息
        List<CustomerLoss> customerLosses=customerLossService.selectAll();
        //将角色信息返回到前台页面
        modelMap.addAttribute("customerLosses",customerLosses);
        return "add.jsp";
    }
    @ResponseBody
    @RequestMapping("/add")
    public MyResult add(CustomerLoss customerLoss, HttpSession session){
        System.out.println("进入控制层方法");
        Users users = (Users) session.getAttribute("users");
        int flag = customerLossService.insert(customerLoss);
        return new MyResult<>(0);
    }
    @RequestMapping("/toupdate")
    public String toupdate(Integer id,ModelMap modelMap){
        CustomerLoss customerLoss = customerLossService.selectById(id);
        modelMap.addAttribute("customerLoss",customerLoss);
        return "update.jsp";
    }
    @ResponseBody
    @RequestMapping("/update")
    public MyResult update(CustomerLoss customerLoss){
        boolean i=customerLossService.update(customerLoss);
        return new MyResult(0);
    }

    @RequestMapping("/tosearch")
    public String tosearch(ModelMap modelMap){
        List<CustomerLoss> customerLosses = customerLossService.selectAll();
        modelMap.addAttribute("customerLosses",customerLosses);
        return "search.jsp";
    }

    @ResponseBody
    @RequestMapping("/customerLossess")
    public MyResult<List<CustomerLoss>> customerLoss(LayRequest layRequest, ModelMap modelMap){
        //执行分页查询
        PageInfo<CustomerLoss> customerPageInfo=customerLossService.selectByPage(layRequest);
        //将分页查询结果传递给页面
        return new MyResult<>(0,customerPageInfo.getTotal(),customerPageInfo.getList());
    }
    @ResponseBody
    @PostMapping("/batchDelete")
    public MyResult batchDelete(Integer [] ids){
        customerLossService.batchDelete(ids);
        return new MyResult(0);
    }

    @ResponseBody//表示返回json数据
    @RequestMapping(value = "/batchdelete",method = RequestMethod.GET)//等同于@GetMapping("/delete")
    public MyResult batchdelete(Integer id){
        boolean flag = customerLossService.delete(id);
        return flag?new MyResult(200,"删除成功！")
                :new MyResult(200,"删除失败");
    }

}
